package com.atguigu.lease.web.admin.controller.login;


import com.atguigu.lease.common.contest.LoginUser;
import com.atguigu.lease.common.contest.LoginUserContest;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.login.LoginContext;

@Tag(name = "后台管理系统登录管理")
@RestController
@RequestMapping("/admin")
public class LoginController {
@Autowired
private LoginService service;
    @Operation(summary = "获取图形验证码")
    @GetMapping("login/captcha")
    public Result<CaptchaVo> getCaptcha() {
        //实现验证码
        //自动发请求
        //响应图片
//有业务逻辑往service放
        CaptchaVo captchaVo = service.getCaptcha();
        return Result.ok(captchaVo);
    }

    @Operation(summary = "登录")
    @PostMapping("login")
    public Result<String> login(@RequestBody LoginVo loginVo) {
        //在这一步仅仅是创建token  解析token  返回页面token存起来
        //建立护照 并没有提取个人信息
        String token = service.login(loginVo);
        return Result.ok(token);
    }

    @Operation(summary = "获取登陆用户个人信息")
    @GetMapping("info")
    public Result<SystemUserInfoVo> info(@RequestHeader("access_token") String token, HttpServletRequest request) {

        LoginUser user = LoginUserContest.getUser();
//        Long userId =(Long) request.getAttribute("userId");
        //提取个人信息  存放在载荷   通过秘钥分析载荷
        System.out.println(user);
        SystemUserInfoVo systemUserInfoVo = service.info(user.getUserId());
        return Result.ok(systemUserInfoVo);
    }
}